package com.pkk.cloud.support.oauth2.center.runner.controller.interior;

import com.pkk.cloud.support.admin.oauth2.api.admin.AdminApi;
import com.pkk.cloud.support.admin.oauth2.api.common.req.ChangePwdReq;
import com.pkk.cloud.support.admin.oauth2.api.common.req.PenguinModifyPasswordReq;
import com.pkk.cloud.support.admin.oauth2.api.common.req.PenguinSysUserNameReq;
import com.pkk.cloud.support.admin.oauth2.api.common.resp.Oauth2UserDetails;
import com.pkk.cloud.support.oauth2.common.utils.UserInfoUtil;
import com.pkk.cloud.support.oauth2.store.handle.OAuth2TokenHandle;
import com.pkk.components.rpc.common.code.CommonCode;
import com.pkk.components.rpc.request.CommonRequest;
import com.pkk.components.rpc.request.RequestHeader;
import com.pkk.components.rpc.response.CommonResponse;
import com.pkk.components.rpc.response.util.ResponseUtil;
import java.security.Principal;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: peikunkun
 * @create: 2020-03-18 15:07
 **/
@Slf4j
@RestController
public class AuthController {

  @Autowired
  private TokenStore tokenStore;
  @Autowired
  private AdminApi adminApi;


  @RequestMapping("test")
  public CommonResponse test(String u, String c) {
    final CommonRequest<PenguinSysUserNameReq> penguinSysUserCommonRequest = new CommonRequest<>();
    penguinSysUserCommonRequest.setBody(PenguinSysUserNameReq.builder().username(u).clientId(c).build());
    return adminApi.loadUserByUsername(penguinSysUserCommonRequest);
  }


  @GetMapping("/current")
  public Principal getUser(Principal principal) {
    return principal;
  }


  /**
   * @Description: 修改密码
   * @Param: request
   * @Param changePwdReq
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Integer>
   * @Author: peikunkun
   * @Date: 2020/3/18 0018 下午 3:07
   */
  @PostMapping(value = "/modifiedUserKey")
  public CommonResponse<Integer> modifiedUserKey(HttpServletRequest request,
      @Validated @RequestBody CommonRequest<ChangePwdReq> changePwdReq) {
    //上下文获取basicUserInfo
    Oauth2UserDetails userInfo = UserInfoUtil.getOAuthUserDetails();
    PenguinModifyPasswordReq password = new PenguinModifyPasswordReq(userInfo.getUsername(),
        changePwdReq.getBody().getOldPassword(),
        changePwdReq.getBody().getNewPassword());
    CommonResponse<?> r = adminApi.modifyUserPwd(new CommonRequest<>(new RequestHeader(), password));
    if (r.getHeader().getCode() instanceof String && CommonCode.SUCCESS.getCode().equals(r.getHeader().getCode())) {
      OAuth2TokenHandle.reMoveToken(tokenStore, request);
      return ResponseUtil.success();
    }
    return ResponseUtil.error((String) r.getHeader().getCode(), r.getHeader().getMessage());
  }

}
